#include "mymusic.h"
#include "ui_mymusic.h"

mymusic::mymusic(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::mymusic)
{
    ui->setupUi(this);
    initUI();
    readFile();
}

mymusic::~mymusic()
{
    delete ui;
}
void mymusic::readFile()
{
  str_pathmusic="D:/BaiduNetdiskDownload/music";
  QDir dir(str_pathmusic);
  QStringList namefilters;
  namefilters<<"*.wav";
  QStringList files=dir.entryList(namefilters,QDir::Files|QDir::Readable,QDir::Name);
  ui->listWidget->addItems(files);
  ui->listWidget->sortItems();
  p_item=ui->listWidget->item(0);
  p_item->setSelected(true);
  /*QMediaPlayer *play=new QMediaPlayer;
  play->setMedia(QUrl::fromLocalFile("D:\\BaiduNetdiskDownload\\music\\Enchanted.wav"));
  play->setVolume(30);
  play->play();*/

  connect(ui->listWidget,SIGNAL(currentRowChanged(int)),this,SLOT(slot_musicListRowNum(int)));


}
#include<QtDebug>
void mymusic::slot_musicListRowNum(int n_number)
{
    qDebug()<<n_number<<endl;
    qDebug()<<ui->listWidget->item(n_number)->text()<<endl;
    str_music=ui->listWidget->item(n_number)->text();
    p_player=new QMediaPlayer;
    p_player->setMedia(QUrl::fromLocalFile(str_pathmusic+'/'+str_music));
    p_player->setVolume(10);
    p_player->stop();
    n_music_number=n_number;

}
void mymusic::initUI()
{
  connect(ui->last,SIGNAL(clicked()),this,SLOT(slots_last_clicked()));
  connect(ui->next,SIGNAL(clicked()),this,SLOT(slots_next_clicked()));
  connect(ui->pause,SIGNAL(clicked()),this,SLOT(slots_pause_clicked()));

}
void mymusic::paintEvent(QPaintEvent *event)
{

    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget,&opt,&p,this);
}

void mymusic::slots_last_clicked()
{
  qDebug()<<"clicked()"<<endl;
  p_player->stop();
  if(n_music_number==0)
  {
      int nmax=ui->listWidget->count();
      str_music=ui->listWidget->item(nmax-1)->text();
      p_item=ui->listWidget->item(nmax-1);
      p_item->setSelected(true);
      QString str=str_pathmusic+'/'+str_music;
      p_player->setMedia(QUrl::fromLocalFile(str));
      p_player->setVolume(10);
      p_player->play();
      n_music_number=nmax-1;

  }
  else
  {
  str_music=ui->listWidget->item(n_music_number-1)->text();
  p_item=ui->listWidget->item(n_music_number-1);
  p_item->setSelected(true);

  QString str=str_pathmusic+'/'+str_music;

  p_player->setMedia(QUrl::fromLocalFile(str));
  p_player->setVolume(10);
  p_player->play();
  n_music_number=n_music_number-1;
}
  ui->pause->setStyleSheet("QPushButton#pause{border-image: url(:/new/prefix1/C:/images/pause_2.png);}");
}
void mymusic::slots_next_clicked()
{
    p_player->stop();
    if(n_music_number==ui->listWidget->count()-1)
    {

        str_music=ui->listWidget->item(0)->text();
        p_item=ui->listWidget->item(0);
        p_item->setSelected(true);
        QString str=str_pathmusic+'/'+str_music;
        p_player->setMedia(QUrl::fromLocalFile(str));
        p_player->setVolume(10);
        p_player->play();
        n_music_number=0;

    }
    else
    {
    str_music=ui->listWidget->item(n_music_number+1)->text();
    p_item=ui->listWidget->item(n_music_number+1);
    p_item->setSelected(true);

    QString str=str_pathmusic+'/'+str_music;

    p_player->setMedia(QUrl::fromLocalFile(str));
    p_player->setVolume(10);
    p_player->play();
    n_music_number=n_music_number+1;
    }
    ui->pause->setStyleSheet("QPushButton#pause{border-image: url(:/new/prefix1/C:/images/pause_2.png);}");
}
void mymusic::slots_pause_clicked()
{
    if(b_play_music==false)
    {
        ui->pause->setStyleSheet("QPushButton#pause{border-image: url(:/new/prefix1/C:/images/pause_2.png);}");
        b_play_music=true;
        p_player->play();
    }
    else
    {
        ui->pause->setStyleSheet("QPushButton#pause{border-image: url(:/new/prefix1/C:/images/pause.png);}");
        b_play_music=false;
        p_player->pause();
    }
}
